package cn.ffcs.bpmpvc.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.echoice.paper.PageBean;
import org.echoice.paper.PageNavi;
import org.echoice.spring.BaseController;
import org.springframework.web.servlet.ModelAndView;

import cn.ffcs.bpmpvc.model.BusinessAnalysis;
import cn.ffcs.bpmpvc.services.BusinessAnalysisService;
import cn.ffcs.bpmpvc.utils.FileUtil;
import cn.ffcs.bpmpvc.web.util.UserContextUtil;
import cn.ffcs.organ.model.Organization;
import cn.ffcs.organ.model.Person;

public class BusinessAnalysisController extends BaseController {
	private final String basePath="/bpm/businessAnalysis/";
	private BusinessAnalysisService businessAnalysisService;
	@Override
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		BusinessAnalysis searchForm=new BusinessAnalysis();
		bindObject(request, searchForm);
		request.setAttribute("searchForm", searchForm);
		PageBean pageBean=getBusinessAnalysisService().getBusinessAnalysisDao().searchBusinessAnalysPage(searchForm, getPageNo(request), getPageSize(request));
		request.setAttribute("dataList", pageBean.getDataList());
		request.setAttribute("pageNaviStr", PageNavi.getPageNaviButton(request, pageBean));
		return new ModelAndView(basePath+"index.jsp");
	}
	
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String appId=request.getParameter("appId");
		BusinessAnalysis businessAnalysis=null;
		if(StringUtils.isNotBlank(appId)){
			businessAnalysis=getBusinessAnalysisService().getBusinessAnalysisDao().getBusinessAnalysis(appId);
			List attachList=getBusinessAnalysisService().getAttachDAO().findAttachList(BusinessAnalysis.class.getName(), appId);
			request.setAttribute("attachList", attachList);
		}else{
			businessAnalysis=new BusinessAnalysis();
			Person person=UserContextUtil.getUserContext(request).getCurrUser().getPerson();
			Organization organization=getBusinessAnalysisService().getAppBusinessReqDao().getOrgByPerson(person.getId());
			businessAnalysis.setOpUser(person.getId().toString());
			businessAnalysis.setOpUserCnName(person.getName());
			businessAnalysis.setOrgCnName(organization.getFullName());		
			businessAnalysis.setOrgId(organization.getId().toString());
		}
		request.setAttribute("businessAnalysis", businessAnalysis);
		return new ModelAndView(basePath+"edit.jsp");
	}
	
	public ModelAndView saveUpload(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		BusinessAnalysis businessAnalysis=new BusinessAnalysis();
		bindObject(request, businessAnalysis);
		Person person=UserContextUtil.getUserContext(request).getCurrUser().getPerson();
		businessAnalysis.setOpUser(person.getId().toString());
		businessAnalysis.setOpTime(new Date());
		String redirectUrl=null;
		String returnMsg="保存成功！";
		try {
			getBusinessAnalysisService().commitSaveUpload(businessAnalysis, request);
		} catch (Exception e) {
			// TODO: handle exception
			returnMsg="保存失败，原因："+e.getMessage();
			logger.info("异常",e);
		}
		if(businessAnalysis.getAppId()!=null){
			redirectUrl="/bpm/opBusinessAnalysis.do?action=showInfo&appId="+businessAnalysis.getAppId();
		}else{
			redirectUrl="/bpm/opBusinessAnalysis.do?action=edit";
		}
		request.setAttribute("returnMsg", returnMsg);
		request.setAttribute("redirectUrl", redirectUrl);
		return new ModelAndView("/bpm/pvc/jumpMsg.jsp");
	}

	public ModelAndView showInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String appId=request.getParameter("appId");
		BusinessAnalysis businessAnalysis=getBusinessAnalysisService().getBusinessAnalysisDao().getBusinessAnalysis(appId);
		List attachList=getBusinessAnalysisService().getAttachDAO().findAttachList(BusinessAnalysis.class.getName(), appId);
		request.setAttribute("listone", businessAnalysis);
		request.setAttribute("attachList", attachList);
		return new ModelAndView(basePath+"show_info.jsp");
	}
	
	public ModelAndView downFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String attachId=request.getParameter("attachId");
		FileUtil.downCommnAttachFile(attachId, response);
		return null;
	}
	
	public ModelAndView del(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String appId=request.getParameter("appId");
		String redirectUrl="/bpm/opBusinessAnalysis.do?action=index";
		String returnMsg="删除成功！！";
		try {
			getBusinessAnalysisService().commitDel(appId);
		} catch (Exception e) {
			// TODO: handle exception
			returnMsg="删除失败，原因："+e.getMessage();
			logger.info("异常",e);
		}
		request.setAttribute("returnMsg", returnMsg);
		request.setAttribute("redirectUrl", redirectUrl);
		return new ModelAndView("/bpm/pvc/jumpMsg.jsp");
	}
	public BusinessAnalysisService getBusinessAnalysisService() {
		return businessAnalysisService;
	}

	public void setBusinessAnalysisService(BusinessAnalysisService businessAnalysisService) {
		this.businessAnalysisService = businessAnalysisService;
	}
	
	
}
